package com.opcode.costa.ui.menu.neworder;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.TreeMap;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Toast;

import com.opcode.costa.R;
import com.opcode.costa.ui.dialogs.FoodDetailDialogFragment;
import com.opcode.costa.ui.dialogs.FoodSelectingDialogFragment;
import com.opcode.costa.ui.menu.MenuOptionsActivity;
import com.opcode.nebulamobil.base.activities.FragmentActivityBase;
import com.opcode.nebulamobil.base.activities.MultiFragmentActivityBase;
import com.opcode.nebulamobil.base.components.ButtonBase;
import com.opcode.nebulamobil.base.components.LinearLayoutBase;
import com.opcode.nebulamobil.base.components.ListViewBase;
import com.opcode.nebulamobil.base.components.TextViewBase;
import com.opcode.nebulamobil.base.fragments.ResponsiveFragmentBase;
import com.opcode.nebulamobil.base.fragments.dialogfragments.OneButtonDialogFragmentBase;
import com.opcode.nebulamobil.base.fragments.dialogfragments.RoundedProgressDialogBase;
import com.opcode.nebulamobil.base.interfaces.OnButtonBaseClickListener;
import com.opcode.nebulamobil.base.interfaces.OnFoodDetailSelectListener;
import com.opcode.nebulamobil.base.interfaces.SelectedFoodListener;
import com.opcode.nebulamobil.information.ApplicationInfo;
import com.opcode.nebulamobil.information.dataforms.costadataforms.ConnectionStatus;
import com.opcode.nebulamobil.information.dataforms.costadataforms.ConnectionStatus.ConnectionStatusInfo;
import com.opcode.nebulamobil.information.dataforms.costadataforms.FoodDataForm;
import com.opcode.nebulamobil.information.requests.costarequests.OrderRequest;
import com.opcode.nebulamobil.information.responses.costaresponses.FoodListResp;
import com.opcode.nebulamobil.information.responses.costaresponses.OrderResponse;
import com.opcode.nebulamobil.utility.JsonConverter;
import com.opcode.nebulamobil.utility.STSLog;
import com.opcode.nebulamobil.utility.UniqueIdCreator;

/**
 * Bu sınıf Yeni Sipariş verilirken pipeline'da ikinci adımı oluşturur. Bu sınıf
 * içerisinden tercih etmek istediğimiz yemekleri seçeriz.
 * 
 * @author Okan ERDOÚAN 31 May 2014
 */
public class NewOrderStepTwoFragment extends ResponsiveFragmentBase implements SelectedFoodListener {

	/*
	 * Bu bölmde sınıf içerisinde kullanacağımız nesne referansları tanımlandı.
	 */
	private View vi;

	/*
	 * Yemek seçimi yapıldıktan sonra seçili yemekleri gösterecek olan liste
	 * tanımlanmıştır. Bu nesne referansının altında yer alan adapter nesnesi de
	 * bu listeyi customize edebilmek iin kullanılmaktadır.
	 */
	private ListViewBase selectedFoodsListView;

	/*
	 * Yemek seçimi yapıldıktan sonra kullanıcıya seçili yemekler
	 * gösterilmektedir.Aşağıdaki adapter nesnesi bu listeyi düzenlemek için
	 * kullanılmaktadÝr.
	 */
	private FoodListAfterAdapter selectedFoodsListAfterAdapter;

	/*
	 * Yemek trlerine gre yemek listesi bu arraylist'lerde tutulmaktadÝr.
	 */
	private ArrayList<FoodDataForm> demoMainFoodArray, demoSoupArray,demoDrinksArray, demoDessertsArray;

	/*
	 * Uygulamada yer alan kaÛÝt efektleri ve diÛer linearlayout'larÝ
	 * ynetebilmek iin nesne referansÝ olußturuluyor.
	 */
	private LinearLayoutBase afterLinearlayout, beforeLinearlayout,paperEffectLayout4, paperEffectLayout5;

	/*
	 * Yemek seimi yapÝlÝrken ilgili tab yapÝsÝ aÝp kapatabilmek iin
	 * aßaÛÝdaki button nesneleri kullanÝlmaktadÝr. Bu buton nesnelerinden
	 * bazÝlarÝ da ißlemi tamamlamak,onaylamak vs. iin kullanÝlacaktÝr.
	 */
	private ButtonBase foodChoosingOkButton, mainFoodChoosingHeaderButton,soupChoosingHeaderButton, drinksChoosingHeaderButton,dessertsChoosingHeaderButton, confirmButton;

	/*
	 * Burada yer alan arraylist nesneleri de seimi yapÝlmÝß olan yemek
	 * listelerini gstermektedir. Her bir trdeki yemekler kendilerine ait
	 * arrayList'lerde tutulmaktadÝrlar.
	 */
	private ArrayList<FoodDataForm> foodsSelectedList, mainFoodSelectedList,soupSelectedList, drinksSelectedList, dessertsSelectedList;

	/*
	 * Ýßlem tamamlandÝÛÝ zaman sunucuya gnderilmek zere hazÝrlanan nesne
	 * referansÝdÝr. Bu nesne json yapÝsÝna dnßtrlerek sunucuya
	 * gnderilecektir.
	 */
	private OrderRequest orderRequest;

	private ArrayList<OrderRequest> orderRequestList;

	private FoodListResp foodListResp;
	
	private RoundedProgressDialogBase roundedProgressDialogBase;


	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
		this.vi = inflater.inflate(R.layout.new_order_step_two_fragment_layout,container, false);

		this.orderRequestList = new ArrayList<OrderRequest>();

		/*
		 * Layout dosyasÝnda olußturulmuß olan grsel Ûeler bu blmde
		 * referanslara atanÝyor.
		 */
		this.beforeLinearlayout = (LinearLayoutBase) this.vi.findViewById(R.id.food_choosing_before_layout);
		this.afterLinearlayout = (LinearLayoutBase) this.vi.findViewById(R.id.food_choosing_after_layout2);
		this.paperEffectLayout4 = (LinearLayoutBase) this.vi.findViewById(R.id.paper_effect_layout_4);
		this.paperEffectLayout5 = (LinearLayoutBase) this.vi.findViewById(R.id.paper_effect_layout_8);

		/*
		 * Seilmiß yemekleri gsterecek olan listview objesi olußturuluyor.
		 */
		this.selectedFoodsListView = (ListViewBase) this.vi.findViewById(R.id.selected_foods_after_listview);

		/*
		 * Üßlemin tamamlama ve onaylama ißlerini yapabilmek iin kullanÝlan
		 * button nesneleridir.
		 */
		this.foodChoosingOkButton = (ButtonBase) this.vi.findViewById(R.id.food_choosing_ok_button);
		this.foodChoosingOkButton.setWaitingState(false);
		
		this.confirmButton = (ButtonBase) this.vi.findViewById(R.id.food_choosing_confirm_button2);

		// Ana Yemekler
		this.mainFoodChoosingHeaderButton = (ButtonBase) this.vi.findViewById(R.id.main_food_choosing_header_button);

		// Çorbalar
		this.soupChoosingHeaderButton = (ButtonBase) this.vi.findViewById(R.id.soup_choosing_header_button);

		// ıecekler
		this.drinksChoosingHeaderButton = (ButtonBase) this.vi.findViewById(R.id.drinks_choosing_header_button);

		// TatlÝlar
		this.dessertsChoosingHeaderButton = (ButtonBase) this.vi.findViewById(R.id.desserts_choosing_header_button);

		this.mainFoodSelectedList = new ArrayList<FoodDataForm>();
		this.soupSelectedList = new ArrayList<FoodDataForm>();
		this.drinksSelectedList = new ArrayList<FoodDataForm>();
		this.dessertsSelectedList = new ArrayList<FoodDataForm>();
		this.foodsSelectedList = new ArrayList<FoodDataForm>();

		this.orderRequest = (OrderRequest) this.getArguments().getSerializable("REQUEST_OBJECT");
		this.foodListResp = (FoodListResp) this.getArguments().getSerializable("RESPONSE_OBJECT");

		/*
		 * Ana Yemeklerin seilmesi ve listView'a yemek listesinin verilmesi ile
		 * ilgili ißlemler
		 */
		if (this.foodListResp != null) {
			this.demoMainFoodArray = new ArrayList<FoodDataForm>();
			this.demoSoupArray = new ArrayList<FoodDataForm>();
			this.demoDrinksArray = new ArrayList<FoodDataForm>();
			this.demoDessertsArray = new ArrayList<FoodDataForm>();

			this.demoMainFoodArray.clear();
			this.demoSoupArray.clear();
			this.demoDrinksArray.clear();
			this.demoDessertsArray.clear();

			try {
				for (int i = 0; i < foodListResp.getFoodList().size(); i++) {
					if (foodListResp.getFoodList().get(i).getFoodType() == FoodDataForm.MAIN_FOODS) {
						this.demoMainFoodArray.add(foodListResp.getFoodList().get(i));
					} else if (foodListResp.getFoodList().get(i).getFoodType() == FoodDataForm.SOUPS) {
						this.demoSoupArray.add(foodListResp.getFoodList().get(i));
					} else if (foodListResp.getFoodList().get(i).getFoodType() == FoodDataForm.DRINKS) {
						this.demoDrinksArray.add(foodListResp.getFoodList().get(i));
					} else if (foodListResp.getFoodList().get(i).getFoodType() == FoodDataForm.DESSERTS) {
						this.demoDessertsArray.add(foodListResp.getFoodList().get(i));
					}
				}
			} catch (NullPointerException exception) {
				STSLog.writeLog(exception, this.getClass().getCanonicalName(),"Yemek listesi alÝnamadÝÛÝndan dolayÝ hata aldÝk.");
			}

		}

		
		// ----------------------------------------------------ANA YEMEK------------------------------------------------
		this.mainFoodChoosingHeaderButton.setOnButtonBaseClickListener(new OnButtonBaseClickListener() {
			
			@Override
			public void onButtonBaseClick(View v) {
				if (demoMainFoodArray != null) {
					FoodSelectingDialogFragment foodSelectingDialogFragment = new FoodSelectingDialogFragment(demoMainFoodArray, FoodDataForm.MAIN_FOODS);
					((FragmentActivityBase) getActivity()).showGeneralDialog(foodSelectingDialogFragment, "FOOD_SELECTING_DIALOG");
				} else {
					Toast.makeText(getActivity(),"Ana yemek listesi alınamadı.Bir önceki ekrana gelip, Tekrar deneyiniz.",Toast.LENGTH_SHORT).show();
				}
	
			}
		});
		// ----------------------------------------------------ANA YEMEK------------------------------------------------

		
		
		
		/*
		 * Çorbaların seçilmesi ve listView'a çorba listesinin verilmesi ile ilgili işlemler---------------------------
		 */

		this.soupChoosingHeaderButton.setOnButtonBaseClickListener(new OnButtonBaseClickListener() {
			
			@Override
			public void onButtonBaseClick(View v) {
				if (demoSoupArray != null) {
					FoodSelectingDialogFragment foodSelectingDialogFragment = new FoodSelectingDialogFragment(demoSoupArray, FoodDataForm.SOUPS);
					((FragmentActivityBase) getActivity()).showGeneralDialog(foodSelectingDialogFragment, "FOOD_SELECTING_DIALOG");

				} else {
					Toast.makeText(getActivity(),"Çorba listesi alınamadı.Bir önceki ekrana gelip, Tekrar deneyiniz.",Toast.LENGTH_SHORT).show();
				}
			}

		});
		// ------------------------------------------------------CORBA--------------------------------------------------

		
		
		
		
		// ----------------------------------------------------İÇECEKLER-------------------------------------------------
		/*
		 * İçeceklerin seilmesi ve listView'a içecek listesinin verilmesi ile ilgili işlemler-------------------------
		 */

		this.drinksChoosingHeaderButton.setOnButtonBaseClickListener(new OnButtonBaseClickListener() {
			
			@Override
			public void onButtonBaseClick(View v) {

				if (demoDrinksArray != null) {
					FoodSelectingDialogFragment foodSelectingDialogFragment = new FoodSelectingDialogFragment(demoDrinksArray,FoodDataForm.DRINKS);		
					((FragmentActivityBase) getActivity()).showGeneralDialog(foodSelectingDialogFragment, "FOOD_SELECTING_DIALOG");
				} else {
					Toast.makeText(getActivity(),"İçecek listesi alınamadı.Bir önceki ekrana gelip, Tekrar deneyiniz.",Toast.LENGTH_SHORT).show();
				}
			}

		});
		// ----------------------------------------------------İÇECEKLER-------------------------------------------------

							
							
		// -----------------------------------------------------TATLILAR------------------------------------------------
				
		/*
		 * Tatlıların seilmesi ve listView'a tatlı listesinin verilmesi ile ilgili işlemler-----------------------------
		 */

		this.dessertsChoosingHeaderButton.setOnButtonBaseClickListener(new OnButtonBaseClickListener() {
			
			@Override
			public void onButtonBaseClick(View v) {

				if (demoDessertsArray != null) {

					FoodSelectingDialogFragment foodSelectingDialogFragment = new FoodSelectingDialogFragment(demoDessertsArray, FoodDataForm.DESSERTS);
					((FragmentActivityBase) getActivity()).showGeneralDialog(foodSelectingDialogFragment, "FOOD_SELECTING_DIALOG");

				} else {
					Toast.makeText(getActivity(),"Tatlı listesi alınamadı.Bir önceki ekrana gelip, Tekrar deneyiniz.",Toast.LENGTH_SHORT).show();
				}
			}
		});
		// -----------------------------------------------------TATLILAR------------------------------------------------

		
		
		
		
		/*
		 * Yemek seme işlemi tamamlandığında TAMAM butonuna basılarak işlem
		 * bitirilir ve pipeline'da bir sonraki ekrana geilir.
		 */
		this.foodChoosingOkButton.setOnButtonBaseClickListener(new OnButtonBaseClickListener() {
			
			@Override
			public void onButtonBaseClick(View v) {

				foodsSelectedList.clear();
				loadSelectedFoodsList(mainFoodSelectedList, foodsSelectedList);
				loadSelectedFoodsList(soupSelectedList, foodsSelectedList);
				loadSelectedFoodsList(drinksSelectedList, foodsSelectedList);
				loadSelectedFoodsList(dessertsSelectedList, foodsSelectedList);

				if (foodsSelectedList.size() != 0) {
					selectedFoodsListAfterAdapter = new FoodListAfterAdapter(getActivity(), R.layout.food_after_selection_item,foodsSelectedList);
					selectedFoodsListView.setAdapter(selectedFoodsListAfterAdapter);

					TreeMap<Long,Integer> orderedFoodList=new TreeMap<Long, Integer>();
					for(int i=0;i<foodsSelectedList.size();i++){
						orderedFoodList.put(foodsSelectedList.get(i).getFoodId(), foodsSelectedList.get(i).getCount());
					}
					orderRequest.setOrderedFoodList(orderedFoodList);

					changeLayoutVisibility(beforeLinearlayout,afterLinearlayout, true);
					foodChoosingOkButton.setVisibility(View.GONE);
					paperEffectLayout4.setVisibility(View.GONE);

					confirmButton.setVisibility(View.VISIBLE);
					paperEffectLayout5.setVisibility(View.VISIBLE);

				} else {
					Toast.makeText(getActivity(),"Yemek seçilmedi. Lütfen yemek menüsünden seçiminizi yapınız.",Toast.LENGTH_LONG).show();
				}
			}
		});

		/*
		 * Seçilen yemekleri onayladığımız ve bir sonraki ekrana geçmemizi sağlayan butondur.
		 */
		this.confirmButton.setOnButtonBaseClickListener(new OnButtonBaseClickListener() {
			
			@Override
			public void onButtonBaseClick(View v) {

				if (foodsSelectedList.size() != 0) {
					createRequest();
				} else {
					Toast.makeText(getActivity(),"Sipariş edilecek yemek bulunamadı.Lütfen siparişlerinizi tekrar oluşturunuz.",Toast.LENGTH_LONG).show();
					backPressed();
				}
			}
		});

		/*
		 * Seilmiß yemeklerin sunucuya gnderilmeden nce son bir defa
		 * kullanÝcÝya gsterildiÛi listedir. Bu ekranda seili yemeklerin
		 * sayÝsÝnda deÛißiklik yapabiliriz. Bunun iin seili yemeÛin zerine
		 * basÝlÝ tutmamÝz ve aÝlan diyalogtan yeni yemek adedini girmemiz
		 * yeterlidir.
		 */
		this.selectedFoodsListView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent,View view, int position, long id) {

				OnFoodDetailSelectListener foodDetailSelectListener = new OnFoodDetailSelectListener() {

					@Override
					public void onFoodDetailSelected(FoodDataForm foodDataForm) {

						for (int i = 0; i < foodsSelectedList.size(); i++) {
							if (foodsSelectedList.get(i).getFoodName().equals(foodDataForm.getFoodName())) {
								foodsSelectedList.set(i, foodDataForm);
							}

							if (foodsSelectedList.get(i).getFoodName().equals(foodDataForm.getFoodName())) {
								if (foodDataForm.getCount() != 0) {
									foodsSelectedList.set(i,foodDataForm);
								} else {
									foodsSelectedList.remove(i);
								}
							}

						}
						
						TreeMap<Long,Integer> orderedFoodList=new TreeMap<Long, Integer>();
						for(int i=0;i<foodsSelectedList.size();i++){
							orderedFoodList.put(foodsSelectedList.get(i).getFoodId(), foodsSelectedList.get(i).getCount());
						}
						orderRequest.setOrderedFoodList(orderedFoodList);

						selectedFoodsListAfterAdapter.notifyDataSetChanged();
					}
				};

				ApplicationInfo.setOnFoodDetailSelectedListener(foodDetailSelectListener);

				FoodDetailDialogFragment foodDetailDialogFragment = new FoodDetailDialogFragment((FoodDataForm) foodsSelectedList.get(position));
				((FragmentActivityBase) getActivity()).showGeneralDialog(foodDetailDialogFragment, "FOOD_SELECTED_DETAIL_DIALOG");

				return false;
			}

		});
		// --------------------------------------------------------------------------------------------------------------

		this.roundedProgressDialogBase=new RoundedProgressDialogBase();

		return this.vi;
	}

	/**
	 * Yemek siparißini sunucuya gndermek iin bu metod aÛÝrÝlÝr. Bu metod
	 * aÛrÝldÝÛÝ zaman hazÝrlanan orderRequest nesnesi sunucuya iletilir.
	 */
	public void createRequest() {

		((FragmentActivityBase) getActivity()).showGeneralDialog(roundedProgressDialogBase, "NEW_ORDER_STEP_TWO_PROGRESS_DIALOG");

		((FragmentActivityBase) getActivity()).createSharedPreferencesManager("sharedpreferences", getActivity());
		ConnectionStatus connectionStatus = (ConnectionStatus) JsonConverter.fromJsonString(((FragmentActivityBase) getActivity()).readFromSharedPreferences("connectionStatus", null),ConnectionStatus.class);

		if (connectionStatus.getConnectionStatusInfo() == ConnectionStatusInfo.CONNECTED) {
			orderRequest.setType("OrderRequest");
			orderRequest.setRequestId(UniqueIdCreator.createUniqueID());
			
		/*	if (orderRequest.getPersonalDataForm() != null) {
				orderRequest.getPersonalDataForm().setPersonelImage(new byte[0]);
			}*/
			ApplicationInfo.getRequestListener().jsonRequestCame(JsonConverter.toJsonString(orderRequest));

		} else {
			Toast.makeText(getActivity(),connectionStatus.getConnectionInfoMessage(),Toast.LENGTH_SHORT).show();
		}

	}

	/**
	 * 
	 */
	public void onResume() {
		super.onResume();
		Log.i("NewstepTwoFragment", "onResume");
		ApplicationInfo.setSelectedFoodListener(this);
		ApplicationInfo.setResponseListener(this);

		((MultiFragmentActivityBase) getActivity()).getPipelineManager().getStepList().get(1).setwFrag(new WeakReference<Fragment>(this));

		((MultiFragmentActivityBase) getActivity()).getPipelineManager().setActiveStep(((MultiFragmentActivityBase) getActivity()).getPipelineManager().getStepList().get(1));
	}

	/**
	 * Bu metod parametre olarak iki adet arraylist alÝr. Bu arrayListlerden bir
	 * tanesi seili btn yemeklerin listesini tutarken diÛeri ise ilgili
	 * kategorideki seili yemekleri tutar. Yani ana yemeklerin tutulduÛu liste
	 * farklÝ, orbalarÝn tutulduÛu liste farklÝ vs. Byle bir durumda farklÝ
	 * arrayListlerde tutulan yemekler tek bir listeye eklenerek sunucuya
	 * iletiliyor.
	 * 
	 * @param foodList
	 * @param selectedFoodList
	 */
	public void loadSelectedFoodsList(ArrayList<FoodDataForm> foodList,ArrayList<FoodDataForm> selectedFoodList) {
		for (int i = 0; i < foodList.size(); i++) {
			selectedFoodList.add(foodList.get(i));
		}
	}

	/**
	 * Bu metodun geriye dndrdÛ deÛer false ise bir nceki fragment'a
	 * geilir. EÛer metodun geri dnß deÛeri true ise aynÝ fragment iindeki
	 * bir nceki duruma (seim yapÝlmamÝß durum) geilir.
	 */
	public boolean backPressed() {
		if (beforeLinearlayout.getVisibility() == View.VISIBLE) {
			((MultiFragmentActivityBase) getActivity()).removeFragmentFromPipeline(((MultiFragmentActivityBase) getActivity()).getPipelineManager(), this);
			return true;
		} else if (afterLinearlayout.getVisibility() == View.VISIBLE) {
			changeLayoutVisibility(beforeLinearlayout, afterLinearlayout, false);
			foodChoosingOkButton.setVisibility(View.VISIBLE);
			paperEffectLayout4.setVisibility(View.VISIBLE);

			confirmButton.setVisibility(View.GONE);
			paperEffectLayout5.setVisibility(View.GONE);
			return false;
		}
		return false;
	}

	/**
	 * Bu sÝnÝf ierisinde zet ekranÝnda yer alan seilmiß yemekler listesi
	 * olußturulur.
	 * 
	 * @author Okan ERDOĞAN 31 May 2014
	 */
	class FoodListAfterAdapter extends ArrayAdapter<FoodDataForm> {

		private Context context;
		private int resourceId;
		private ArrayList<FoodDataForm> arrayList;

		public FoodListAfterAdapter(Context context, int textViewResourceId,ArrayList<FoodDataForm> objects) {
			super(context, textViewResourceId, objects);

			this.context = context;
			this.resourceId = textViewResourceId;

			this.arrayList = objects;
		}

		public View getView(int position, View convertView, ViewGroup parent) {

			try {
				convertView = LayoutInflater.from(this.context).inflate(this.resourceId, null);
				FoodDataForm foodListAfterDataForm = (FoodDataForm) arrayList.get(position);

				TextViewBase selectedFoodName = (TextViewBase) convertView.findViewById(R.id.selected_food_name);
				TextViewBase selectedFoodCount = (TextViewBase) convertView.findViewById(R.id.selected_food_count);
				TextViewBase foodPriceTextview = (TextViewBase) convertView	.findViewById(R.id.food_price_textview);

				int count = 0;

				TextViewBase specialty_name1 = (TextViewBase) convertView.findViewById(R.id.specialty_name1);
				TextViewBase specialty_value1 = (TextViewBase) convertView.findViewById(R.id.specialty_value1);

				for (int i = 0; i < foodListAfterDataForm.getSelectablePortions().length; i++) {
					if (foodListAfterDataForm.getSelectablePortions()[i] != null) {
						specialty_name1.setText(foodListAfterDataForm.getSelectablePortions()[i].getName());

						specialty_value1	.setText(""+ foodListAfterDataForm.getSelectablePortions()[i].getCount());

						count = foodListAfterDataForm.getSelectablePortions()[i]
								.getCount();
					} else {
						specialty_name1.setVisibility(View.GONE);
						specialty_value1.setVisibility(View.GONE);
					}
				}

				TextViewBase specialty_name2 = (TextViewBase) convertView.findViewById(R.id.specialty_name2);
				TextViewBase specialty_value2 = (TextViewBase) convertView.findViewById(R.id.specialty_value2);
				specialty_name2.setVisibility(View.GONE);
				specialty_value2.setVisibility(View.GONE);

				TextViewBase specialty_name3 = (TextViewBase) convertView.findViewById(R.id.specialty_name3);
				TextViewBase specialty_value3 = (TextViewBase) convertView.findViewById(R.id.specialty_value3);
				specialty_name3.setVisibility(View.GONE);
				specialty_value3.setVisibility(View.GONE);

				selectedFoodName.setText(foodListAfterDataForm.getFoodName());

				selectedFoodCount.setText("" + count);

				foodPriceTextview.setText("Fiyat: "	+ String.valueOf(count	* foodListAfterDataForm.getPrice()) + " TL");

			} catch (InflateException e) {
				STSLog.writeLog(e, this.getClass().getCanonicalName(),"Verilen layout dosyasından view elde edilirken hata alındı.");
			}
			return convertView;
		}

		public int getCount() {
			return arrayList.size();
		}

		public void notifyDataSetChanged() {
			super.notifyDataSetChanged();
			arrayList = foodsSelectedList;
		}

	}

	/**
	 * Yemek seimi yapÝldÝÛÝ zaman seili yemeklerin listesi buraya dßer.
	 */
	@Override
	public void foodSelected(ArrayList<FoodDataForm> selectedFoodList,
			int foodCategorie) {

		if (foodCategorie == FoodDataForm.MAIN_FOODS) {
			this.mainFoodSelectedList = selectedFoodList;

			/*
			 * EÛer ana yemek listesinin eleman sayÝsÝ 0 ise yemek listesi ilk
			 * durumuna gndrlyor.
			 */
			if (mainFoodSelectedList.size() <= 0) {
				for (int i = 0; i < demoMainFoodArray.size(); i++) {
					for (int j = 0; j < demoMainFoodArray.get(i).getSelectablePortions().length; j++) {
						if (demoMainFoodArray.get(i).getSelectablePortions()[j] != null) {
							demoMainFoodArray.get(i).getSelectablePortions()[j].setCount(0);
							demoMainFoodArray.get(i).setSelected(false);
						}
					}
				}
			}

			/*
			 * EÛer ana yemek listesinin eleman sayÝsÝ sÝfÝrdan farklÝ ise,
			 * yemek seim ekranÝna yemek listesine yemek bilgileri
			 * gnderilirken Seimi yapÝlan porsiyon bilgileriyle gnderiliyor.
			 */
			for (int i = 0; i < mainFoodSelectedList.size(); i++) {
				for (int j = 0; j < demoMainFoodArray.size(); j++) {
					if (mainFoodSelectedList.get(i).getFoodName().equals(demoMainFoodArray.get(j).getFoodName())) {
						demoMainFoodArray.set(j, mainFoodSelectedList.get(i));
					}
				}

			}

		} else if (foodCategorie == FoodDataForm.SOUPS) {
			this.soupSelectedList = selectedFoodList;

			/*
			 * EÛer orba listesinin eleman sayÝsÝ 0 ise yemek listesi ilk
			 * durumuna gndrlyor.
			 */
			if (soupSelectedList.size() <= 0) {
				for (int i = 0; i < demoSoupArray.size(); i++) {
					for (int j = 0; j < demoSoupArray.get(i)	.getSelectablePortions().length; j++) {
						if (demoSoupArray.get(i).getSelectablePortions()[j] != null) {
							demoSoupArray.get(i).getSelectablePortions()[j].setCount(0);
							demoSoupArray.get(i).setSelected(false);
						}

					}
				}
			}

			/*
			 * EÛer orba listesinin eleman sayÝsÝ sÝfÝrdan farklÝ ise, yemek
			 * seim ekranÝna yemek listesine yemek bilgileri gnderilirken
			 * Seimi yapÝlan porsiyon bilgileriyle gnderiliyor.
			 */
			for (int i = 0; i < soupSelectedList.size(); i++) {
				for (int j = 0; j < demoSoupArray.size(); j++) {
					if (soupSelectedList.get(i).getFoodName().equals(demoSoupArray.get(j).getFoodName())) {
						demoSoupArray.set(j, soupSelectedList.get(i));
					}
				}

			}
		} else if (foodCategorie == FoodDataForm.DRINKS) {
			this.drinksSelectedList = selectedFoodList;

			/*
			 * EÛer iecek listesinin eleman sayÝsÝ 0 ise yemek listesi ilk
			 * durumuna gndrlyor.
			 */
			if (drinksSelectedList.size() <= 0) {
				for (int i = 0; i < demoDrinksArray.size(); i++) {
					for (int j = 0; j < demoDrinksArray.get(i).getSelectablePortions().length; j++) {
						if (demoDrinksArray.get(i).getSelectablePortions()[j] != null) {
							demoDrinksArray.get(i).getSelectablePortions()[j].setCount(0);
							demoDrinksArray.get(i).setSelected(false);
						}

					}
				}
			}

			/*
			 * EÛer iecek listesinin eleman sayÝsÝ sÝfÝrdan farklÝ ise, yemek
			 * seim ekranÝna yemek listesine yemek bilgileri gnderilirken
			 * Seimi yapÝlan porsiyon bilgileriyle gnderiliyor.
			 */
			for (int i = 0; i < drinksSelectedList.size(); i++) {
				for (int j = 0; j < demoDrinksArray.size(); j++) {
					if (drinksSelectedList.get(i).getFoodName().equals(demoDrinksArray.get(j).getFoodName())) {
						demoDrinksArray.set(j, drinksSelectedList.get(i));
					}
				}

			}
		} else if (foodCategorie == FoodDataForm.DESSERTS) {
			this.dessertsSelectedList = selectedFoodList;

			/*
			 * EÛer tatlÝ listesinin eleman sayÝsÝ 0 ise yemek listesi ilk
			 * durumuna gndrlyor.
			 */
			if (dessertsSelectedList.size() <= 0) {
				for (int i = 0; i < demoDessertsArray.size(); i++) {
					for (int j = 0; j < demoDessertsArray.get(i).getSelectablePortions().length; j++) {
						if (demoDessertsArray.get(i).getSelectablePortions()[j] != null) {
							demoDessertsArray.get(i).getSelectablePortions()[j].setCount(0);
							demoDessertsArray.get(i).setSelected(false);
						}
					}
				}
			}

			/*
			 * EÛer tatlÝ listesinin eleman sayÝsÝ sÝfÝrdan farklÝ ise, yemek
			 * seim ekranÝna yemek listesine yemek bilgileri gnderilirken
			 * Seimi yapÝlan porsiyon bilgileriyle gnderiliyor.
			 */
			for (int i = 0; i < dessertsSelectedList.size(); i++) {
				for (int j = 0; j < demoDessertsArray.size(); j++) {
					if (dessertsSelectedList.get(i).getFoodName().equals(demoDessertsArray.get(j).getFoodName())) {
						demoDessertsArray.set(j, dessertsSelectedList.get(i));
					}
				}

			}
		}
	}

	@Override
	public void responseCame(String response) {
		OrderResponse orderResponse = (OrderResponse) JsonConverter.fromJsonString(response, OrderResponse.class);
		if (orderResponse.getType().equals("OrderResponse")) {
			if (orderResponse.isOrderResult()) {
				Intent orderIntent = new Intent(getActivity(),MenuOptionsActivity.class);
				if ((FragmentActivityBase) getActivity() != null) {
					((FragmentActivityBase) getActivity()).closeGeneralDialog(roundedProgressDialogBase, "NEW_ORDER_STEP_TWO_PROGRESS_DIALOG");
				}
				OneButtonDialogFragmentBase oneButtonDialogFragmentBase=new OneButtonDialogFragmentBase("İşlem Başarılı",orderIntent);
				((FragmentActivityBase) getActivity()).showGeneralDialog(oneButtonDialogFragmentBase, "NEW_ORDER_STEP_TWO_PROCESS_COMPLETED_DIALOG");
			} else {
				if ((FragmentActivityBase) getActivity() != null) {
					((FragmentActivityBase) getActivity()).closeGeneralDialog(roundedProgressDialogBase, "NEW_ORDER_STEP_TWO_PROGRESS_DIALOG");
				}       
				OneButtonDialogFragmentBase oneButtonDialogFragmentBase=new OneButtonDialogFragmentBase("İşlem Başarısız oldu.");
				((FragmentActivityBase) getActivity()).showGeneralDialog(oneButtonDialogFragmentBase, "ADD_ORDER_STEP_TWO_PROCESS_COMPLETED_DIALOG");
			}
		}

	}
}
